<template>
  <AmisFormComponent :formContext="amisFormContextObj" :formData="props.amisFormDataObj" ref="amisFormComponent" />
</template>

<script lang="ts" setup>
import * as FormApi from '@/api/bpm/form'
import {string} from "vue-types";
import {AmisFormContext} from "@/components/AmisFormComponent/amisFormContext";
import {getGetParams} from "@/utils/url";


defineOptions({name: 'AmisFormShowByKey'})

const route = useRoute() // 路由信息
const schema = ref()
const amisFormContextObj = ref<AmisFormContext>({}) // amisForm 的 上下文数据


console.log("route-AmisFormShowByKey",route)


const props = defineProps({
  formReadOnly: {
    type: Boolean,
    required:false,
    default: () => {
      return false
    }
  },
  hideButtons: {
    type: Boolean,
    required:false,
    default: () => {
      return false
    }
  },
  amisFormShowFormKey: {
    type: string,
    default: () => {
      return null
    }
  },
  amisFormDataObj: {
    type: Object ,
    required: false,
    default: ()=>{
      return {};
    }
  }
})

const amisFormComponent = ref(null)

/** 初始化 **/
onBeforeMount(async () => {
  console.log("onMounted...")
  // 上层组件传递过来的
  if(!!props.amisFormShowFormKey){
    amisFormContextObj.value.amisFormKey = props.amisFormShowFormKey
    amisFormContextObj.value.formReadOnly = props.formReadOnly
    amisFormContextObj.value.hideButtons = props.hideButtons
  }
  // 路由元数据
  else if(!!route.meta.query?.AmisFormShowFormKey){
    amisFormContextObj.value.amisFormKey = route.meta.query.AmisFormShowFormKey
    amisFormContextObj.value.formReadOnly = route.meta.query.formReadOnly
    amisFormContextObj.value.hideButtons = route.meta.query.hideButtons
  }
  // url上获取
  else {
    let getParams = getGetParams(location.href);
    amisFormContextObj.value.amisFormKey = getParams.get("amisFormShowFormKey")
    amisFormContextObj.value.formReadOnly = getParams.get("formReadOnly")
    amisFormContextObj.value.hideButtons = getParams.get("hideButtons")
  }

  console.log(amisFormContextObj.value,'amisFormContextObj')
})
onActivated(()=>{
  console.log("onActivated...")
  let formData = amisFormComponent.value?.getFormDate()
  console.log("当前表单数据",formData)
})
onUnmounted(async () => {
  console.log("onUnmounted...")
  amisFormComponent.value?.onAmisUnmounted()
})
</script>
